﻿using Microsoft.AspNetCore.Mvc;
using MyApp.Repository;
using ReportDesignerServer.Common;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.DataModels;
using ReportDesignerServer.Dto;
using ReportDesignerServer.IRepository;
using SqlSugar;
using System.Data;

namespace ReportDesignerServer.Services
{
    /// <summary>
    /// 护士工作量服务
    /// </summary>
    public class NurseDayWorkService
    {
        private INurseDayWorkRepository nurseDayWorkRepository;

        private AdoRepository adoRepository;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nurseDayWorkRepository"></param>
        /// <param name="adoRepository"></param>
        public NurseDayWorkService(INurseDayWorkRepository nurseDayWorkRepository, AdoRepository adoRepository)
        {
            this.nurseDayWorkRepository = nurseDayWorkRepository;
            this.adoRepository = adoRepository;
        }


        /// <summary>
        /// 通过月份组查询数据
        /// </summary>
        /// <returns></returns>

        public Rt GetNurseDayWorksByMouth(string mouth)
        {
            Expressionable<NurseDayWork> exp = Expressionable.Create<NurseDayWork>();
            exp.And(it => it.GROUPNAME == mouth);
            var list = nurseDayWorkRepository.GetListByWhere(exp);
            return Rt.SuccessObj(list);
        }

        /// <summary>
        /// 通过id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public Rt GetNurseDayWorkById(string id)
        {
            long lid = 0;
            if (long.TryParse(id, out lid))
            {
                Expressionable<NurseDayWork> exp = Expressionable.Create<NurseDayWork>();
                exp.And(it => it.ID == lid);
                var model = nurseDayWorkRepository.GetListByWhere(exp).FirstOrDefault();
                return Rt.SuccessObj(model);
            }
            else
            {
                return Rt.Error();
            }
        }


        /// <summary>
        /// 通过月份组查询数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Rt SaveNurseDayWork(NurseDayWork model)
        {
            long res = 0;
            if (model != null)
            {
                if (string.IsNullOrEmpty(model.WORKTYPE))
                    return Rt.Fail("请选择班次！");
                if (model.ID == 0)
                {
                    Expressionable<NurseDayWork> exp = Expressionable.Create<NurseDayWork>();
                    exp.And(it => it.RECORD_TIME.Date == model.RECORD_TIME.Date);
                    exp.And(it => it.WORKTYPE == model.WORKTYPE);
                    var list = nurseDayWorkRepository.GetListByWhere(exp);
                    if (list == null || list.Count == 0)
                    {
                        //沒有查到同一天,同一班次的数据
                        //生成组名
                        model.GROUPNAME = model.RECORD_TIME.ToString("yyyy-MM");
                        //记录添加时间
                        model.CREATE_TIME = DateTime.Now;
                        res = nurseDayWorkRepository.ExecuteReturnSnowflakeId(model);
                    }
                    else
                    {
                        return Rt.Fail($"【{model.RECORD_TIME.ToString("yyyy-MM-dd")}】,【{model.WORKTYPE}】数据已存在，请刷新界面！");
                    }
                }
                else
                {
                    res = nurseDayWorkRepository.Update(model);
                }
            }
            if (res > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }

        /// <summary>
        /// 获取列配置字典
        /// </summary>
        /// <returns></returns>
        public List<ColShow> GetDictCol()
        {
            var list = adoRepository.DB.Queryable<ColShow>().OrderBy(it => it.Sort, OrderByType.Asc).ToList();
            list.ForEach(c =>
            {
                c.SID = c.Id.ToString();
            });
            return list;
        }


        /// <summary>
        /// 批量保存排序
        /// </summary>
        /// <returns></returns>
        public int SaveDictColSort(List<ColShow> lst)
        {
            lst.ForEach(c =>
            {
                long lid = 0;
                if (long.TryParse(c.SID,out lid))
                    c.Id = lid;
            });
            int res = adoRepository.DB.Updateable(lst).UpdateColumns(it => new { it.Sort ,it.IfShow}).ExecuteCommand();
            return res;
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public List<NurseDayWork> GetYearList(string year)
        {
            List<NurseDayWork> yearList = adoRepository.DB.Queryable<NurseDayWork>().Where(c => SqlFunc.Like(c.GROUPNAME, year + "%")).GroupBy(c => c.GROUPNAME).Select((row) => new NurseDayWork()
            {
                GROUPNAME = row.GROUPNAME,
                C1 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C1), 0, int.Parse(row.C1))).ToString(),
                C2 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C2), 0, int.Parse(row.C2))).ToString(),
                C3 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C3), 0, int.Parse(row.C3))).ToString(),
                C4 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C4), 0, int.Parse(row.C4))).ToString(),
                C5 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C5), 0, int.Parse(row.C5))).ToString(),
                C6 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C6), 0, int.Parse(row.C6))).ToString(),
                C7 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C7), 0, int.Parse(row.C7))).ToString(),
                C8 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C8), 0, int.Parse(row.C8))).ToString(),
                C9 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C9), 0, int.Parse(row.C9))).ToString(),
                C10 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C10), 0, int.Parse(row.C10))).ToString(),
                C11 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C11), 0, int.Parse(row.C11))).ToString(),
                C12 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C12), 0, int.Parse(row.C12))).ToString(),
                C13 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C13), 0, int.Parse(row.C13))).ToString(),
                C14 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C14), 0, int.Parse(row.C14))).ToString(),
                C15 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C15), 0, int.Parse(row.C15))).ToString(),
                C16 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C16), 0, int.Parse(row.C16))).ToString(),
                C17 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C17), 0, int.Parse(row.C17))).ToString(),
                C18 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C18), 0, int.Parse(row.C18))).ToString(),
                C19 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C19), 0, int.Parse(row.C19))).ToString(),
                C20 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C20), 0, int.Parse(row.C20))).ToString(),
                C21 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C21), 0, int.Parse(row.C21))).ToString(),
                C22 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C22), 0, int.Parse(row.C22))).ToString(),
                C23 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C23), 0, int.Parse(row.C23))).ToString(),
                C24 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C24), 0, int.Parse(row.C24))).ToString(),
                C25 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C25), 0, int.Parse(row.C25))).ToString(),
                C26 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C26), 0, int.Parse(row.C26))).ToString(),
                C27 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C27), 0, int.Parse(row.C27))).ToString(),
                C28 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C28), 0, int.Parse(row.C28))).ToString(),
                C29 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C29), 0, int.Parse(row.C29))).ToString(),
                C30 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C30), 0, int.Parse(row.C30))).ToString(),
                C31 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C31), 0, int.Parse(row.C31))).ToString(),
                C32 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C32), 0, int.Parse(row.C32))).ToString(),
                C33 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C33), 0, int.Parse(row.C33))).ToString(),
                C34 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C34), 0, int.Parse(row.C34))).ToString(),
                C35 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C35), 0, int.Parse(row.C35))).ToString(),
                C36 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C36), 0, int.Parse(row.C36))).ToString(),
                C37 = SqlFunc.AggregateSum(SqlFunc.IIF<int>(string.IsNullOrEmpty(row.C37), 0, int.Parse(row.C37))).ToString(),
            }).ToList();

            return yearList;
        }

        /// <summary>
        /// 获取datatable 数据
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public DataTable LoadData(string year)
        {

            List<NurseDayWork> yearList = GetYearList(year);
            List<ColShow> listCol = GetDictCol();
            DataTable dt = new DataTable();
            //-------------------------数据列添加-----14列------
            dt.Columns.Add(new DataColumn() { ColumnName = "项目" });
            for (int i = 1; i <= 12; i++)
            {
                DataColumn column = new DataColumn();
                column.ColumnName = i.ToString() + "月";
                dt.Columns.Add(column);
            }
            dt.Columns.Add(new DataColumn() { ColumnName = "汇总" });
            //------------------------------------
            //行添加
            for (int i = 0; i < listCol.Count; i++)
            {
                DataRow dr = dt.NewRow();
                //第一个单元格
                dr[0] = listCol[i].Header;
                int rowSum = 0;
                for (int j = 1; j <= 12; j++)
                {
                    string mouth = new DateTime(int.Parse(year), j, 1).ToString("yyyy-MM");
                    var temp = yearList.FindAll(c => c.GROUPNAME == mouth).FirstOrDefault();
                    if (temp != null)
                        dr[j] = ExtendHelper.FindProValue<string>("C" + (i + 1).ToString(), temp);
                    else
                        dr[j] = "0";
                    rowSum += dr[j].ToSafeString().ToInt();
                }
                dr[13] = rowSum;
                //最后一个单元格
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
